<template>
   
  <mdp-dialog append-to-body width="60%" top="20px" ref="parentDialog">
    <template #default> 
      <XmPhase showConfirm subOpType="select"
      :sel-project="selProject"
      @confirm="(task)=>onParentTaskSelected(task)"
    />
    </template>
  </mdp-dialog>

  <mdp-dialog   width="80%" ref="menuDialog" title="选中需要导入的需求【如果不选具体的需求，则全量导入】">
    <template #default> 
      <mdp-lr-box :lspan="12" lborder rborder>
        <template #left> 
          <XmMenuTree ref="sourceMenuRef"  subOpType="select" multiple :sel-project="selProject"
            /> 
        </template> 
          <el-form label-width="120px" label-position="left" class="padding-left">
            <el-form-item label="源头数据">
                <el-radio v-model="tpl.sourceType" label="all">产品下所有需求项</el-radio> 
                <el-radio v-model="tpl.sourceType" label="some">手工选中的需求项</el-radio>
            </el-form-item> 
            <el-form-item label="目标位置">
              <el-radio v-model="tpl.targetType" label="top">当前项目顶级位置</el-radio> 
              <el-radio v-model="tpl.targetType" label="sub">某个阶段/规划之下</el-radio>
            </el-form-item> 
            
            <el-form-item label="新的上级" v-if="tpl.targetType=='sub'">
              {{tpl.parentTaskid}} &nbsp;<el-button @click="$refs['parentDialog'].open()">选择新的上级</el-button>
            </el-form-item> 
            <el-form-item label="数据处理"> 
              所有日期数据向后延申 &nbsp;<el-input-number v-model="tpl.days"/> 天
            </el-form-item>
          </el-form>
          <div class="footer">
            <el-button type="primary" @click="importFromMenu()" :disabled="load.import">提交</el-button>
          </div>
      </mdp-lr-box> 

    </template>

  </mdp-dialog> 
</template>

<script>
import util from '@/components/mdp-ui/js/util' //全局公共库

import { 
  batchImportTaskFromMenu, 
} from '@/api/xm/core/xmTask'
 
 //新增界面 


import { mapState } from 'pinia'
import { useUserStore } from '@/store/modules/user'
 
import { ElMessageBox } from 'element-plus'
 
export default {
  props: [ 'selProject'],
  
  emits:['importMenu'],
  computed: {
    ...mapState(useUserStore, ['userInfo', 'roles']),
     
  },
  watch: { 
  },
  data() {
    return {
      load:{import:false},  
       
      tpl:{
        sourceProductId:'',//源头产品
        sourceType:'some',// all-全部，some-选中的
        targetType:'top' ,  // 目标位置 top-顶级，sub-作为某个阶段、规划的子集 
        parentTaskid:'' ,  // 新的上级编号，顶级为0
        days:0, // 新的日期向后延申天数
        projectId:'',//目标项目编号
      }
    }
  }, //end data
  methods: { 
    open(){
      this.$refs['menuDialog'].open();
    },
    onParentTaskSelected(task){
      if(task?.id){
        if(task.ptype>2){
          this.$notify.warning('【'+task.name+'】属于任务，不允许有下级工作项，请选【阶段/规划】')
          return;
        }
        this.tpl.parentTaskid=task.id
        this.tpl.targetParentTaskName=task.name
        this.$refs['parentDialog'].close();
      }

    },
    importFromMenu(){ 
      
      let tplMenus=this.$refs['sourceMenuRef'].getCheckedNodes()
      let tplProduct=this.$refs['sourceMenuRef'].product
      if(!tplProduct?.id){
        this.$notify.warning('请选择待导入的产品')
        return;
      }
      if(this.tpl.sourceType=='some' && tplMenus.length==0){
        this.$notify.warning('请选择至少一个待导入的需求项')
        return;
      }
      if(this.tpl.targetType=='sub' && !this.tpl.parentTaskid){
        this.$notify.warning('请选择目标位置')
        return;
      } 
      this.load.import=true;
      let params={...this.tpl,xmMenus:tplMenus,projectId:this.selProject.id,sourceProductId:tplProduct.id }
      batchImportTaskFromMenu(params).then(res=>{
        var tips = res.tips;
        this.load.import=false;
        if(tips.isOk){
          this.$refs['menuDialog'].close();
          this.$emit('importMenu',params)
          this.$notify.success('导入成功')
        }else{
          this.$notify.error(tips.msg)
        }
      })
      
    }
  }, //end methods
  mounted() {
  }
}
</script>
 
